import pymysql
import requests
from bs4 import BeautifulSoup

conn=pymysql.connect(host='127.0.0.1',
        user='deng',
        password='7112',
        database='learn',
        charset='utf8')
cursor=conn.cursor()

#sql='create table learn1(id int auto_increment,name varchar(100),age int not null,created datetime,primary key(id))'
#cursor.close()
#conn.close()
def geturl(url):
    html=requests.get(url)
    bs=BeautifulSoup(html.text,'html.parser')
    return bs

#https://www.runoob.com/mysql/mysql-tutorial.html
url=input('输入抓取首页')
bs=geturl(url)
print(bs.title.get_text())
list1=bs.find('div',{'id':'leftcolumn'}).find_all('a')
for i in list1:
    i=i['title']
    i='Mysql'+i[6:]
    sql='insert into Mysqllearn (title) values (%s);'
    cursor.execute(sql,[i])
    print(i)

conn.commit()
cursor.close()
conn.close()
